#include "XSubMesh.h"
#include "../XModEngine/XRender.h"


XSubMesh::XSubMesh()
{
	//m_vertex_count = 0; m_vertex_begin = -1; m_vertex_prim_count = 0;
	ptr_tex = NULL;
}


XSubMesh::~XSubMesh()
{

}

bool XSubMesh::Init(){
	m_sections.clear();
	return true;
}

void XSubMesh::Render(){
	//XRender::GetInstance()->SetMaterial(m_mat);
	if (ptr_tex!=NULL)
		XRender::GetInstance()->SetTexture(ptr_tex);
	//Turn on Alpha blend
	XRender::GetInstance()->SetRenderMode(6);
	for each (Section var in m_sections)
	{
		if (var.m_vertex_count > 0 && var.m_vertex_begin >= 0 && var.m_vertex_prim_count > 0)
			XRender::GetInstance()->DrawObject(0, 0, 0, var.m_vertex_count, var.m_vertex_begin, var.m_vertex_prim_count);
	}
	//Turn offf Alpha blend
	XRender::GetInstance()->SetRenderMode(7);
}


string XSubMesh::ToString(){
	return "XMod::XSubMesh";
}



void XSubMesh::SetTexture(string str_file){
	XModFileStream *tex_file = new XModFileStream(str_file);
	tex_file->SetAutoRec();
	if (!XRender::GetInstance()->CreateTexture(&ptr_tex))
		return;
	ptr_tex->LoadResource(*tex_file);
	tex_file->Release();
}

void XSubMesh::SetMaterial(XMaterial material){
	m_mat = material;
}

void XSubMesh::AddSection(){
	Section new_section;
	m_sections.insert(m_sections.end(), new_section);
}